package com.zxx.xin.shorthand.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.zxx.xin.shorthand.R;
import com.zxx.xin.shorthand.database.DatabaseHelper;
import com.zxx.xin.shorthand.helper.DateHelper;
import com.zxx.xin.shorthand.helper.PermissionHelper;
import com.zxx.xin.shorthand.ui.widget.VerticalTextView;

/**
 * Created by xin on 2018/3/20.
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String[] Permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

    private  PermissionHelper mPermissionHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((VerticalTextView)findViewById(R.id.time)).setText(DateHelper.getChineseTimeInMillis(System.currentTimeMillis()));

        findViewById(R.id.read).setOnClickListener(this);
        findViewById(R.id.write).setOnClickListener(this);

        mPermissionHelper = new PermissionHelper(this);
        mPermissionHelper.requestPermissions(Permissions);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.read:
                if(DatabaseHelper.getInstance().listAllDaily() != null && DatabaseHelper.getInstance().listAllDaily().size() > 0){
                    startActivity(new Intent(this, DailyListActivity.class));
                } else {
                    DailyWriteActivity.startPage(this, null);
                }
                break;

            case R.id.write:
                DailyWriteActivity.startPage(this, null);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == PermissionHelper.requestCode){
            mPermissionHelper.requestPermissions(permissions);
        }
    }
}
